
package com.primecodegen.common.AttributesProperties;

import com.primecodegen.common.MainProject.MainProjectDAO;
import com.primecodegen.common.MainProject.MainProjectTable;
import com.primecodegen.util.*;
import com.primecodegen.common.base.BaseBean;

import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.logging.*;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import javax.servlet.http.HttpSession;
import org.primefaces.component.commandbutton.CommandButton;

@ManagedBean(name = "attributesPropertiesBean")
@RequestScoped
public class AttributesPropertiesBean extends BaseBean implements Serializable {

    // *********** Local Variables/Objects ***********
    private static final Logger logger = Logger.getLogger(AttributesPropertiesBean.class.getName());
    private int count;
    private int attributesPropertiesID;
    private String excludedClmn;
    private AttributesPropertiesTable selectedAttributesProperties;
    HttpSession session;
    private AttributesPropertiesDAO attributesPropertiesDAO;
    private ArrayList<AttributesPropertiesTable> attributesPropertiesList;
    private AttributesPropertiesTable attributesPropertiesSrchCrit;
    private AttributesPropertiesTable attributesPropertiesUpdate;
    private AttributesPropertiesTable attributesPropertiesTable;

    FacesContext context;
    ColToOrderBy colToOrderBy;

    // *********** Constructors ***********
    public AttributesPropertiesBean() {
        logger.info(" Constructor AttributesPropertiesBean()");
        excludedClmn="6";
        context = getContext();
        session = getSession();
        attributesPropertiesDAO = new AttributesPropertiesDAO();
        attributesPropertiesList = new ArrayList<AttributesPropertiesTable>();
        attributesPropertiesTable = new AttributesPropertiesTable();
        attributesPropertiesSrchCrit = (AttributesPropertiesTable) session.getAttribute("sessionAttributesPropertiesSrchCrit");
        attributesPropertiesUpdate = (AttributesPropertiesTable) session.getAttribute("attributesPropertiesUpdateSess");
        colToOrderBy = (ColToOrderBy) session.getAttribute("sessionColToOrderBy");

        if (attributesPropertiesSrchCrit == null) {
            attributesPropertiesSrchCrit = new AttributesPropertiesTable();
        }

        if (attributesPropertiesUpdate == null) {
            attributesPropertiesUpdate = new AttributesPropertiesTable();
        }



        fillAttributesPropertiesList();
    }


    // *********** Setters & Getters ***********
    public String getExcludedClmn() {
        return excludedClmn;
    }

    public void setExcludedClmn(String excludedClmn) {
        this.excludedClmn = excludedClmn;
    }

    public AttributesPropertiesTable getAttributesPropertiesUpdate() {
        attributesPropertiesUpdate = (AttributesPropertiesTable) session.getAttribute("attributesPropertiesUpdateSess");
         if (attributesPropertiesUpdate == null) {
            attributesPropertiesUpdate = new AttributesPropertiesTable();
        }
        return attributesPropertiesUpdate;
    }

    public void setAttributesPropertiesUpdate(AttributesPropertiesTable attributesPropertiesUpdate) {
        session.setAttribute("attributesPropertiesUpdateSess", attributesPropertiesUpdate);
        this.attributesPropertiesUpdate = attributesPropertiesUpdate;
    }



    public AttributesPropertiesBean(AttributesPropertiesTable attributesPropertiesTable) {
        logger.info("Constructor AttributesPropertiesBean( AttributesPropertiesTable attributesPropertiesTable )");
        this.attributesPropertiesTable = attributesPropertiesTable;
    }

    public AttributesPropertiesTable getAttributesPropertiesTable() {
        return attributesPropertiesTable;
    }

    public void setAttributesPropertiesTable(AttributesPropertiesTable attributesPropertiesTable) {
        this.attributesPropertiesTable = attributesPropertiesTable;
    }

    public AttributesPropertiesTable getAttributesPropertiesSrchCrit() {
        return attributesPropertiesSrchCrit;
    }

    public void setAttributesPropertiesSrchCrit(AttributesPropertiesTable attributesPropertiesSrchCrit) {
        this.attributesPropertiesSrchCrit = attributesPropertiesSrchCrit;
    }

    public AttributesPropertiesTable getSelectedAttributesProperties() {
        return selectedAttributesProperties;

    }

    public void setSelectedAttributesProperties(AttributesPropertiesTable selectedAttributesProperties) {
        this.selectedAttributesProperties = selectedAttributesProperties;
    }

    public ArrayList<AttributesPropertiesTable> getAttributesPropertiesList() {
        return attributesPropertiesList;
    }
//******************************
    private ArrayList<SelectItem> mainProjectList = new ArrayList<SelectItem>();
    private MainProjectDAO mainProjectDAO = new MainProjectDAO();
    public ArrayList<SelectItem> getMainProjectList() {
        getAllMainProject();
        return mainProjectList;
    }

    public void setMainProjectList(ArrayList<SelectItem> mainProjectList) {
        this.mainProjectList = mainProjectList;
    }

    public void getAllMainProject() {
        mainProjectList = new ArrayList<SelectItem>();
        MainProjectTable groupTable = new MainProjectTable();
        mainProjectList.add(new SelectItem("", "- Select Role -"));
        try {
            Iterator itMainProject = mainProjectDAO.getByCriterionOrderedBy(null, null).iterator();
            while (itMainProject.hasNext()) {
                groupTable = (MainProjectTable) itMainProject.next();
                mainProjectList.add(new SelectItem(groupTable.getID(), groupTable.getName()));
                groupTable = new MainProjectTable();
            }

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in public void getAllMainProject() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }
    //******************************

    // *********** Actions ***********
    public void doFilter() {
        logger.log(Level.INFO, "Starting: public void doFilter () in AttributesPropertiesBean");
        try {
            attributesPropertiesSrchCrit.setFilter(true);
            this.attributesPropertiesList = attributesPropertiesDAO.getByCriterionOrderedBy(attributesPropertiesSrchCrit, null);

            session.setAttribute("sessionAttributesPropertiesSrchCrit", attributesPropertiesSrchCrit);
            fillAttributesPropertiesList();
            logger.log(Level.INFO, "Finishing: public void doFilter () in AttributesPropertiesBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in doFilter() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }

        public void clearFilter(){
        try {
            attributesPropertiesSrchCrit = null;
            session.setAttribute("sessionAttributesPropertiesSrchCrit",attributesPropertiesSrchCrit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sortByID() {

        logger.log(Level.INFO, "public void fillAttributesPropertiesList ()");

        try {

            if (attributesPropertiesSrchCrit != null && colToOrderBy != null) {
                this.attributesPropertiesList = attributesPropertiesDAO.getByCriterionOrderedBy(attributesPropertiesSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.attributesPropertiesList = attributesPropertiesDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (attributesPropertiesSrchCrit != null) {
                this.attributesPropertiesList = attributesPropertiesDAO.getByCriterionOrderedBy(attributesPropertiesSrchCrit, null);
            } else {
                this.attributesPropertiesList = attributesPropertiesDAO.getByCriterionOrderedBy(null, null);
            }




        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillAttributesPropertiesList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }

    public void update() {
        try {
            int parID = new Integer(session.getAttribute("attributesPropertiesIDSess").toString());
            attributesPropertiesUpdate.setID(parID);
            attributesPropertiesDAO.updateByID(attributesPropertiesUpdate);

        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured interface the update action, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in Update(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void Insert() {
        logger.info("public void Insert()");
        try {

            int isInserted = -1;


            isInserted = attributesPropertiesDAO.insert(attributesPropertiesTable);

            if (isInserted != -1) {
                getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved Successfully", "AttributesProperties Added"));
                fillAttributesPropertiesList();
            } else {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "error", "error shit"));
                logger.log(Level.WARNING, "Exception occured in Insert(). Error Message: isInserted != -1 Probably exception occure in attributesPropertiesDAO.insert( attributesPropertiesTable )");

            }

        } catch (Exception e) {

            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void delete() {
        try {

            int parID = new Integer(session.getAttribute("attributesPropertiesIDSess").toString());



            attributesPropertiesDAO.deleteByID(parID);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "AttributesProperties Deleted"));
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure", "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
        }
    }

    public void setIDinSession() {
        try {
            Map<String, String> params = context.getExternalContext().getRequestParameterMap();
            int parID = new Integer(params.get("attributesPropertiesID"));
            attributesPropertiesUpdate = (AttributesPropertiesTable) attributesPropertiesDAO.getByID(parID);
            session.setAttribute("attributesPropertiesUpdateSess", attributesPropertiesUpdate);
            session.setAttribute("attributesPropertiesIDSess", parID);
        } catch (Exception ex) {
            Logger.getLogger(AttributesPropertiesBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void fillAttributesPropertiesList() {
        logger.log(Level.INFO, "Starting: public void fillAttributesPropertiesList () in AttributesPropertiesBean");
        try {

            if ((attributesPropertiesSrchCrit != null && attributesPropertiesSrchCrit.isFilter()) && colToOrderBy != null) {
                this.attributesPropertiesList = attributesPropertiesDAO.getByCriterionOrderedBy(attributesPropertiesSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.attributesPropertiesList = attributesPropertiesDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (attributesPropertiesSrchCrit != null && attributesPropertiesSrchCrit.isFilter()) {
                this.attributesPropertiesList = attributesPropertiesDAO.getByCriterionOrderedBy(attributesPropertiesSrchCrit, null);
            } else {
                this.attributesPropertiesList = attributesPropertiesDAO.getByCriterionOrderedBy(null, null);
            }



            logger.log(Level.INFO, "finishing: public void fillAttributesPropertiesList () in AttributesPropertiesBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillAttributesPropertiesList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }

    public void updateRow(ActionEvent e) {
            CommandButton d =(CommandButton) e.getSource();

             Map<String, String> params = context.getExternalContext().getRequestParameterMap();

            int parID = new Integer(params.get("ID"));
            String parDescr = params.get("newDescr").toString();
            System.out.println("ID= "+parID + " Descr= "+parDescr);
             context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "PASSED Successfully","ID= "+parID + " Descr= "+parDescr));
     }


}

